#include "scene.h"
#include <QtWidgets>
//类似于初始化
scene::scene(QMenu *itemMenu,QObject *parent):QGraphicsScene(parent){
    myItemMenu= itemMenu;
    myMode    = MoveItem;
    line      = 0;
    textItem  = 0;
    myItemColor = Qt::blue;
    myItemColor = Qt::blue;
    myTextColor = Qt::black;
    myLineColor = Qt::black;
    myDataManger=new mDataManager();
}

scene::~scene()
{

}

//设置线的颜色
void scene::setLineColor(const QColor &color)
{
    myLineColor = color;
    if (isItemChange(mArrow::Type)) {
        mArrow *item =
            qgraphicsitem_cast <mArrow *>(selectedItems().first());
        item->setColor(myLineColor);
        update();
    }
}
//设置文本颜色
void scene::setTextColor(const QColor &color){
    myTextColor = color;
    if(isItemChange(mText::Type)){
        mText *item =
                    qgraphicsitem_cast<mText *>(selectedItems().first());
                item->setDefaultTextColor(myTextColor);
   }

}
//设置item颜色
void scene::setItemColor(const QColor &color){
    myItemColor = color;
    if (isItemChange(mText::Type)) {
        mProcess *item =
            qgraphicsitem_cast<mProcess *>(selectedItems().first());
        item->setBrush(myItemColor);
    }
}
//设置字体颜色
void scene::setFont(const QFont &font)
{
    myFont = font;

    if (isItemChange(mText::Type)) {
        mText *item =
            qgraphicsitem_cast<mText *>(selectedItems().first());
        //At this point the selection can change so the first selected item might not be a DiagramTextItem
        if (item)
            item->setFont(myFont);
    }
}
void scene::setMode(Mode mode)
{
    myMode = mode;
}

void scene::editorLostFocus(mText *item)
{
    QTextCursor cursor = item->textCursor();
    cursor.clearSelection();
    item->setTextCursor(cursor);

    if (item->toPlainText().isEmpty()) {
        removeItem(item);
        item->deleteLater();
    }
}
//判断当前状态是否改变
bool scene::isItemChange(int type)
{
    foreach (QGraphicsItem *item, selectedItems()) {
        if (item->type() == type)
            return true;
    }
    return false;
}



bool scene::deleteItemFromData(int id)
{
    myDataManger->DeleteProcess(id);
    return true;
}

bool scene::deleteArrowFromData(int id)
{
    myDataManger->DeleteArrow(id);
    return true;
}

bool scene::saveDataToXMLFile(QString path)
{
    return myDataManger->SaveFile(path);
}

void scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (mouseEvent->button() != Qt::LeftButton)
        return;

    QString butterfly_fn;
    QString Itemext="";

    butterfly_fn="C:/PNG/Add2.png";
    Itemext="AirPatrol";

    //ImageItem* imageItem;
    mProcess *item;
    QPointF posPointer;

    switch (myMode) {
    case InsertItem:
        item = new mProcess(myDataManger->GetProcessID());
        item->setBrush(myItemColor);
        addItem(item);

        item->setPos(mouseEvent->scenePos());
        item->setZValue(1000);
        emit itemInserted(item);

    /*	butterflyimg = new QImage[4];
        butterflyimg[0].load( butterfly_fn );
        imageItem = new ImageItem(butterflyimg[0],item);
        addItem(imageItem);	*/

    /*	posPointer.setX(mouseEvent->scenePos().x()-24);
        posPointer.setY(mouseEvent->scenePos().y()-24);
        imageItem->setPos(posPointer);
        imageItem->setZValue(1);
        item->addImage(imageItem);*/

        textItem = new mText(item);
        textItem->setFont(myFont);
        textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
        textItem->setZValue(1000);
        connect(textItem, SIGNAL(lostFocus(DiagramTextItem*)),
            this, SLOT(editorLostFocus(DiagramTextItem*)));
        connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
            this, SIGNAL(itemSelected(QGraphicsItem*)));
        addItem(textItem);


        textItem->setDefaultTextColor(myTextColor);

        posPointer.setX(mouseEvent->scenePos().x()-24-butterfly_fn.length()*2);
        posPointer.setY(mouseEvent->scenePos().y()+24);
        textItem->setPos(posPointer);
        textItem->setPlainText(Itemext);

        item->addText(textItem);
        //emit textInserted(textItem);

        myDataManger->AddProcess(item->GetMyId(),item->pos(),Itemext,myTextColor);

        break;
        //! [6] //! [7]
    case InsertLine:
        line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
            mouseEvent->scenePos()));
        line->setPen(QPen(myLineColor, 2));
        addItem(line);
        break;
        //! [7] //! [8]
    case InsertText:
        textItem = new mText(item);
        textItem->setFont(myFont);
        textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
        textItem->setZValue(1000.0);
        connect(textItem, SIGNAL(lostFocus(DiagramTextItem*)),
            this, SLOT(editorLostFocus(DiagramTextItem*)));
        connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
            this, SIGNAL(itemSelected(QGraphicsItem*)));
        addItem(textItem);
        textItem->setDefaultTextColor(myTextColor);
        textItem->setPos(mouseEvent->scenePos());
        emit textInserted(textItem);
        //! [8] //! [9]
    default:
        ;
    }
    QGraphicsScene::mousePressEvent(mouseEvent);
}

void scene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (myMode == InsertLine && line != 0) {
        QLineF newLine(line->line().p1(), mouseEvent->scenePos());
        line->setLine(newLine);
    }
    else if (myMode == MoveItem) {
        QGraphicsScene::mouseMoveEvent(mouseEvent);
    }
}

void scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (line != 0 && myMode == InsertLine) {
        QList<QGraphicsItem *> startItems = items(line->line().p1());
        if (startItems.count() && startItems.first() == line)
            startItems.removeFirst();
        QList<QGraphicsItem *> endItems = items(line->line().p2());
        if (endItems.count() && endItems.first() == line)
            endItems.removeFirst();

        removeItem(line);
        delete line;
        //! [11] //! [12]

        if (startItems.count() > 0 && endItems.count() > 0 &&
            startItems.first()->type() == mProcess::Type &&
            endItems.first()->type() == mProcess::Type &&
            startItems.first() != endItems.first()) {
                mProcess *startItem =
                    qgraphicsitem_cast<mProcess*>(startItems.first());
                mProcess *endItem =
                    qgraphicsitem_cast<mProcess *>(endItems.first());
                mArrow *arrow = new mArrow(startItem, endItem,myDataManger->GetArrowID());
                arrow->setColor(myLineColor);
                startItem->addArrow(arrow);
                endItem->addArrow(arrow);
                arrow->setZValue(-1000.0);
                addItem(arrow);
                myDataManger->AddArrow(arrow->GetMyID(),startItem->GetMyId(),endItem->GetMyId(),myLineColor);
                arrow->updatePosition();
        }
    }
    //! [12] //! [13]
    line = 0;
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}

void scene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    event->accept();
}

void scene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    mProcess *item;
    QPointF posPointer;
    QByteArray itemData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
    QString mTreeText=itemData;

    switch (myMode) {
    case MoveItem:
        item = new mProcess(myDataManger->GetProcessID());
        item->setBrush(myItemColor);
        addItem(item);

        item->setPos(event->scenePos());
        item->setZValue(1000);
        emit itemInserted(item);

        /*	butterflyimg = new QImage[4];
        butterflyimg[0].load( butterfly_fn );
        imageItem = new ImageItem(butterflyimg[0],item);
        addItem(imageItem);	*/

        /*	posPointer.setX(mouseEvent->scenePos().x()-24);
        posPointer.setY(mouseEvent->scenePos().y()-24);
        imageItem->setPos(posPointer);
        imageItem->setZValue(1);
        item->addImage(imageItem);*/

        textItem = new mText(item);
        textItem->setFont(myFont);
        textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
        textItem->setZValue(1000);
        connect(textItem, SIGNAL(lostFocus(DiagramTextItem*)),
            this, SLOT(editorLostFocus(DiagramTextItem*)));
        connect(textItem, SIGNAL(selectedChange(QGraphicsItem*)),
            this, SIGNAL(itemSelected(QGraphicsItem*)));
        addItem(textItem);
        textItem->setDefaultTextColor(myTextColor);

        posPointer.setX(event->scenePos().x()-24);
        posPointer.setY(event->scenePos().y()+24);
        textItem->setPos(posPointer);
        textItem->setPlainText(mTreeText);

        item->addText(textItem);
        //emit textInserted(textItem);

        //myDataManger->AddProcess(item->GetMyId(),item->pos(),Itemext,myTextColor);

        break;
    }
    //event->setDropAction(Qt::MoveAction);
    event->accept();

}
